package cn.imvc.app.models;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Brand;
import cn.imvc.app.entities.BrandGoodsClass;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsClass;
import cn.imvc.app.repositories.BrandGoodsClassRepository;
import cn.imvc.app.repositories.GoodsRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年8月4日下午2:35:20
 *
 */
@Service
public class BrandGoodsClassModelImpl implements BrandGoodsClassModel{

	@Autowired
	BrandGoodsClassRepository brandGoodsClassRepository;
	@Autowired
	RefRelationModel refRelationModel;
	@Autowired
	BrandModel brandModel;
	@Autowired
	GoodsClassModel goodsClassModel;
	@Autowired
	GoodsRepository goodsRepository;
	
	public Page<BrandGoodsClass> find(ViewRequest viewRequest) {
		
		return brandGoodsClassRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public BrandGoodsClass get(int id) {
		
		return brandGoodsClassRepository.getOne(id);
	}

	public int save(int goodsClassId,int brandId ,BrandGoodsClass brandGoodsClass) {
		GoodsClass goodsClass = brandGoodsClass.getGoodsClass();
		String brandName = brandGoodsClass.getBrandName();
		List<BrandGoodsClass> bn = brandGoodsClassRepository.getByGoodsClassAndBrandName(goodsClass,brandName);
		if(bn.size()>0){
			return -1;
		}
		brandGoodsClassRepository.saveAndFlush(brandGoodsClass);
		return 1;
	}

	public int update(int id, int goodsClassId,int brandId , BrandGoodsClass brandGoodsClass) {
		
		BrandGoodsClass loadBrandGoodsClass = brandGoodsClassRepository.getOne(id);
		GoodsClass goodsClass = brandGoodsClass.getGoodsClass();
		String brandName = brandGoodsClass.getBrandName();
		List<BrandGoodsClass> bn = brandGoodsClassRepository.getByGoodsClassAndBrandName(goodsClass,brandName);
		if(bn.size()>0&&!brandName.equals(loadBrandGoodsClass.getBrandName())){
			return -1;
		}
		BeanUtils.copyProperties(brandGoodsClass, loadBrandGoodsClass, new String[]{"id"});
		brandGoodsClassRepository.saveAndFlush(loadBrandGoodsClass);
		return 1;
	}

	public void delete(int id) {
		
	  brandGoodsClassRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			brandGoodsClassRepository.delete(id);
		}
		
	}

	/**
	 * 根据商品的品牌和分类的选择，自动更新品牌关系关联表。
	 * ！该功能的使用应放在商品更新保存前。
	 * @param oper 0:删，1：增改
	 */
	public void updateRelation(Goods goods,int oper) {
		List<BrandGoodsClass> brandGoodsClasses = brandGoodsClassRepository.findByGoodsClassAndBrand(goods.getGoodsClass(), goods.getBrand());
		if(goods.getId() != 0){
			Goods goodsTmp = goodsRepository.findOne(goods.getId());
			//如果更新的品牌类别关联在商品中不存在了，则将品牌类别关联表中的关联数据给删了。
			if(goodsTmp.getBrand().getId() != goods.getBrand().getId() || goodsTmp.getGoodsClass().getId() != goods.getGoodsClass().getId() || oper == 0){
				if(goodsRepository.findByGoodsClassAndBrand(goodsTmp.getGoodsClass(), goodsTmp.getBrand()).size() == 1){
					brandGoodsClassRepository.deleteByGoodsClassAndBrand(goodsTmp.getGoodsClass(), goodsTmp.getBrand());
				}
			}
		}
		//如果是新添加的品牌类别关联，则向品牌类别关联表添加一条关联数据。
		if(brandGoodsClasses.isEmpty()){
			BrandGoodsClass brandGoodsClass = new BrandGoodsClass();
			Brand brand = brandModel.get(goods.getBrand().getId());
			GoodsClass goodsClass = goodsClassModel.get(goods.getGoodsClass().getId());
			brandGoodsClass.setBrand(brand);
			brandGoodsClass.setBrandName(brand.getName());
			brandGoodsClass.setGoodsClass(goodsClass);
			brandGoodsClass.setGoodsClassName(goodsClass.getName());
			brandGoodsClassRepository.saveAndFlush(brandGoodsClass);
		}
	}

	

}
